package com.thirdgroup.service.impl;

import com.thirdgroup.dao.CusInfoDao;
import com.thirdgroup.dao.IndustryDao;
import com.thirdgroup.dao.impl.CusInfoDaoImpl;
import com.thirdgroup.dao.impl.IndustryDaoImpl;
import com.thirdgroup.pojo.entity.BusinessCustomerIndustry;
import com.thirdgroup.pojo.entity.CustomerInfo;
import com.thirdgroup.pojo.entity.ManagerInfo;
import com.thirdgroup.pojo.vo.CustomerInfoVo;
import com.thirdgroup.service.CusInfoService;
import com.thirdgroup.utils.MD5Util;
import com.thirdgroup.utils.respond.R;

import java.util.ArrayList;
import java.util.List;

public class CusInfoServiceImpl implements CusInfoService {
    private CusInfoDao cusInfoDao=new CusInfoDaoImpl();
    private IndustryDao industryDao=new IndustryDaoImpl();
    @Override
    public R selectAll() {
        List<CustomerInfo> customerInfos = cusInfoDao.selectAll();
        List<CustomerInfoVo> customerInfoVos=new ArrayList<>();
        List<BusinessCustomerIndustry> businessCustomerIndustries = industryDao.selectName();
        if (customerInfos != null && !"".equals(customerInfos)){
            for (CustomerInfo customerInfo : customerInfos) {
                CustomerInfoVo customerInfoVo = new CustomerInfoVo();
                customerInfoVo.setCustomerid(customerInfo.getCustomerid());
                customerInfoVo.setCustomerusername(customerInfo.getCustomerusername());
                customerInfoVo.setCustomerpassword(customerInfo.getCustomerpassword());
                customerInfoVo.setCustomername(customerInfo.getCustomername());
                customerInfoVo.setCustomerbalance(customerInfo.getCustomerbalance());
                customerInfoVo.setCustomerstatus(customerInfo.getCustomerstatus());
                for (BusinessCustomerIndustry industry : businessCustomerIndustries) {
                    if (industry.getIndustryid().equals(customerInfo.getCustomerindustryid())) {
                        customerInfoVo.setCustomerindustryid(industry.getIndustryname());
                        break; // 找到匹配的行业后，跳出循环
                    }
                }
                customerInfoVo.setCreatetime(customerInfo.getCreatetime());
                customerInfoVo.setUpdatetime(customerInfo.getUpdatetime());
                customerInfoVo.setProfilepicture(customerInfo.getProfilepicture());
                customerInfoVos.add(customerInfoVo);
                System.out.println("打印每一条客户信息：");
                System.out.println(customerInfoVo);
            }
            System.out.println("Vo赋值成功，返回到servlet");
            return R.ok(customerInfoVos);
        }
        return R.error(500,"无数据");
    }

    @Override
    public R updateState(String id, String status,String date) {
        Integer integer = cusInfoDao.updateState(id, status,date);
        if (integer>=0){
            System.out.println("成功修改"+integer+"条数据！");
            return R.ok("成功修改"+integer+"条数据！");
        }
        System.out.println("系统繁忙，请稍后再试~");
        return R.error(500,"系统繁忙，请稍后再试~");
    }

    @Override
    public R delete(String customerId) {
        Integer integer = cusInfoDao.delete(customerId);
        if (integer>=0){
            System.out.println("删除"+integer+"条数据！");
            return R.ok("删除"+integer+"条数据！");
        }
        System.out.println("系统繁忙，请稍后再试~");
        return R.error(500,"系统繁忙，请稍后再试~");
    }

    @Override
    public R detail(String customerId) {
        CustomerInfo integer = cusInfoDao.detail(customerId);
        if (integer!=null&&!"".equals(integer)){
            Integer customerindustryid = integer.getCustomerindustryid();
            if (customerindustryid!=null&&customerindustryid!=0){
                CustomerInfoVo infoVo=new CustomerInfoVo();
                infoVo.setCustomerid(integer.getCustomerid());
                infoVo.setCustomerusername(integer.getCustomerusername());
                infoVo.setCustomerpassword(integer.getCustomerpassword());
                infoVo.setCustomername(integer.getCustomername());
                infoVo.setCustomerbalance(integer.getCustomerbalance());
                infoVo.setCustomerstatus(integer.getCustomerstatus());
                String s = industryDao.selectById(customerindustryid);
                infoVo.setCustomerindustryid(s);
                infoVo.setCreatetime(integer.getCreatetime());
                infoVo.setUpdatetime(integer.getUpdatetime());
                infoVo.setProfilepicture(integer.getProfilepicture());
                System.out.println("查找到数据！");
            return R.ok(infoVo);
            }else{
                return R.ok(integer);
            }
        }
        System.out.println("系统繁忙，请稍后再试~");
        return R.error(500,"系统繁忙，请稍后再试~");
    }

    @Override
    public R add(String id, String username, String password, String name, String industry, String formattedDate, String profilePicture) {
        String encrypt = MD5Util.encrypt(password);
        Integer integer1 = industryDao.selectByName(industry);
        Integer integer = cusInfoDao.add(id, username,encrypt,name,integer1,formattedDate,profilePicture);
        if (integer>=0){
            System.out.println("成功添加"+integer+"条数据！");
            return R.ok("成功添加"+integer+"条数据！");
        }
        System.out.println("系统繁忙，请稍后再试~");
        return R.error(500,"系统繁忙，请稍后再试~");
    }
}
